<template>
	<view class="wxnumber">
		<!-- 	<view class="title">
			添加微信账号
		</view>
		<view class="sm">
			请绑定本人微信账户
		</view>
		<view class="inputBox">
			<view class="lable">
				真实姓名
			</view>
			<input class="inputitem" type="text" placeholder="请输入真实姓名" />
			<view class="lable" style="margin-top: 10rpx;">
				手机号
			</view>
			<input class="inputitem" type="text" placeholder="请输入手机号" />
		</view> -->
		<view class="uptext">
			上传收款码
		</view>
		
		<image v-if="wx_code == null" src="@/static/image/upload.png" mode=""
			style="width: 202rpx;height: 202rpx;margin-top: 20rpx;margin-left: 30rpx;" @click="handleAvatar"></image>
		<image :src="wx_code" mode=""
			style="width: 202rpx;height: 202rpx;margin-top: 20rpx;margin-left: 30rpx;" @click="handleAvatar"></image>

		<view class="btn" @click="handleSave">
			提交
		</view>

	</view>
</template>

<script>
	import {
		userinfo,checkuser
	} from "@/api/api.js"
	export default {
		data() {
			return {
				wx_code: null,
			}
		},
		onLoad() {
			if (!uni.getStorageSync("token")) {
				uni.reLaunch({
					url: "../login/index"
				})
				return
			}
			this.handleGetuserinfo()
		},
		methods: {
			async handleGetuserinfo() {
				let res = await userinfo()
				this.wx_code = res.data.wx_code
				console.log("用户信息", res)
			},
			
			handleAvatar() {
				let that = this
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], //从相册选择
					success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths;
						console.log("tempFilePaths", tempFilePaths)
						uni.uploadFile({
							url: 'http://admin.jinsekeji.com.cn/api/login/upload', //仅为示例，非真实的接口地址
							filePath: tempFilePaths[0],
							name: 'file',
							header: {
								// "Content-Type": "application/form-data",
								"Authorization": 'Bearer' + ' ' + that.token
							},
							success: (uploadFileRes) => {
								// console.log("33333", uploadFileRes)
								that.wx_code = (JSON.parse(uploadFileRes.data).data)
								console.log("图片", that.wx_code)
							},
							fail(e) {
								console.log("上传图片", e)
							}
						});
					}
				});
			},
			
			async handleSave(){
				let res = await checkuser({
					wx_code:this.wx_code
				})
				if(res.code == 1000){
					uni.showToast({
						title: "微信收款码上传成功！",
						icon: "none",
						duration: 1500
					});
					setTimeout(()=>{
						uni.navigateBack({delta:1})
					},2000)
				}else{
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 1500
					});
				}
			},

		}
	}
</script>

<style scoped lang="scss">
	.wxnumber {
		.title {
			font-size: 28rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 400;
			color: #333333;
			line-height: 28rpx;
			margin-left: 30rpx;
			margin-top: 20rpx;
		}

		.sm {
			font-size: 28rpx;
			font-family: PingFang SC-Regular, PingFang SC;
			font-weight: 400;
			color: #707070;
			line-height: 28rpx;
			margin-left: 30rpx;
			margin-top: 10rpx;
		}

		.inputBox {
			width: 100%;
			height: 296rpx;
			background: #FFFFFF;
			padding: 20rpx 30rpx;
			box-sizing: border-box;
			margin-top: 10rpx;

			.lable {
				font-size: 30rpx;
				font-family: PingFang SC-Medium, PingFang SC;
				font-weight: 400;
				color: #333333;
				line-height: 42rpx;
			}

			.inputitem {
				width: 690rpx;
				height: 76rpx;
				background: #F1F1F1;
				text-indent: 20rpx;
				margin-top: 10rpx;
			}
		}

		.uptext {
			font-size: 30rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 400;
			color: #333333;
			line-height: 42rpx;
			margin-top: 18rpx;
			margin-left: 30rpx;
		}

		.btn {
			width: 542rpx;
			height: 80rpx;
			background: #04767C;
			border-radius: 512rpx 512rpx 512rpx 512rpx;
			font-size: 36rpx;
			font-family: PingFang SC-Bold, PingFang SC;
			font-weight: 400;
			color: #FFFFFF;
			line-height: 80rpx;
			text-align: center;
			margin: 0 auto;
			margin-top: 142rpx;
		}
	}
</style>